﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleTest
{
    class Anonymous
    {
        /// <summary>
        /// 匿名方法简单测试
        /// </summary>
        public static void  AnonyDelegate()
        {
            //delegate后面的括号可加可不加
            ConsoleDelegate del= delegate { Console.WriteLine(" this is a anonymous method"); };

            del(1);
        }
        public static void AnonyMethodWithRef()
        {

            AnonyDelegateWithRef del = delegate(ref int refPara, out int outPara) { System.Console.WriteLine("refPara is {0} ", refPara);
            refPara++;
            outPara = 20;

            return 0;
            };

            int refVal = 5;
            int outVal;

            del(ref refVal, out outVal);
        }

        private static DelagateTypeCounter MakeCounter()
        {
            int counter=0;
            DelagateTypeCounter delegateInstanceCounter = delegate { return ++counter; };
            return delegateInstanceCounter;
        }
        public static void DelegateCounter()
        {
            DelagateTypeCounter delegateCounter1 = MakeCounter();
            DelagateTypeCounter delegateCounter2 = MakeCounter();
            System.Console.WriteLine(delegateCounter1());
            System.Console.WriteLine(delegateCounter1());
            System.Console.WriteLine(delegateCounter2());
            System.Console.WriteLine(delegateCounter2());

        }

    }
    public delegate void ConsoleDelegate(int a);
    public delegate int AnonyDelegateWithRef(ref int refPara,out int outPara);
    public delegate int DelagateTypeCounter();
}
